package org.example.controller;

import org.example.model.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/request")
public class GetRequestDataController {

    @RequestMapping("/1")
    public String test1(String name){
        return "接收到的数据name="+name;
    }

    @RequestMapping("/2")
    public String test2(User user){
        return user.toString();
    }

    @RequestMapping("/3")
    public String test3(String id, User user){
        return "id="+id+", "+user.toString();
    }

    @RequestMapping("/4")
    public String test4(@RequestParam String name){
        return "接收到的数据name="+name;
    }

    @RequestMapping("/5")
    public String test5(@RequestParam(required = false) String name){
        return "接收到的数据name="+name;
    }

    @RequestMapping("/6")
    public String test6(@RequestParam("xxx") String name){
        return "接收到的数据name="+name;
    }

    @RequestMapping("/7")
    public String test7(@RequestBody User user){
        return user.toString();
    }

    @RequestMapping("/getUser/{id}")
    public String test8(@PathVariable Integer id){
        User u = new User();
        u.setUsername(String.valueOf(id));
        return u.toString();
    }

    @RequestMapping("/8")
    public String test8(Integer id, @RequestPart MultipartFile f){
        System.out.println("id="+id+", f="+f.getOriginalFilename());
        return "id="+id+", f="+f.getOriginalFilename();
    }

    @RequestMapping("/9")
    public String test9(HttpServletRequest request, HttpServletResponse response){
        System.out.println(request.getParameter("name"));
        return request.getParameter("name");
    }

    @RequestMapping("/10")
    public String test10(@CookieValue("sid") String sid){
        return sid;
    }

    @RequestMapping("/11")
    public String test11(@RequestHeader("User-Agent") String userAgent){
        return userAgent;
    }

    @RequestMapping("/12")//模拟登录接口，登录成功使用session保存用户信息
    public String test12(HttpSession session){
        session.setAttribute("username", "abc");
        return session.toString();
    }

    @RequestMapping("/13")
    public String test13(@SessionAttribute("username") String username){
        return username;
    }
}
